<!-- LEFT -->
 <?php //echo $this->loadTemplate('left', 'default') ?>
 <?php include 'components/com_frmessage/views/bg/tmpl/default_left.php'; ?>
 <!-- END LEFT -->
 
 <!-- CONTENT -->
 <div id="jm-content">       
    <h3 class="jm-title">フォルダ管理<span class="float-right"><button id="folder_add" style="margin-top: -1px;">新規</button></span></h3>
     
    <div class="cls">
        <table border="1" id="tb_blue">
            <thead>
    			<tr>
    				<th style="width: 30%;">フォルダ</th>
    				<th style="width: 15%;">メッセージ数	</th>
    				<th style="width: 15%;">未読</th>
                    <th style="width: 40%;">アクション</th>
    			</tr>
    		</thead> 

    		<tbody>
      		    <tr class="row0">
                    <td>受信トレイ</td>
                    <td class="center"><?php echo $this->mainFolders->inbox->read + $this->mainFolders->inbox->unread ?></td>
                    <td class="center"><?php echo $this->mainFolders->inbox->unread ?></td>
                    <td class=""><button style="margin-top: 0px;float: right;" onclick="clear_folder('inbox', '受信トレイ')">空にする</button></td>
                </tr>
                <tr class="row0">
                    <td>送信済み</td>
                    <td class="center"><?php echo $this->mainFolders->sent->read ?></td>
                    <td class="center">-</td>
                    <td class=""><button style="margin-top: 0px;float: right;" onclick="clear_folder('sent', '送信済み')">空にする</button></td>
                    
                </tr>
               
                <tr class="row0">
                    <td> 下書き</td>
                    <td class="center"><?php echo $this->mainFolders->draft->read ?></td>
                    <td class="center">-</td>
                    <td class=""><button style="margin-top: 0px;float: right;" onclick="clear_folder('draft', '下書き')">空にする</button></td>
                </tr>
                 <tr class="row0">
                    <td>ゴミ箱</td>
                    <td class="center"><?php echo $this->mainFolders->trash->read +  $this->mainFolders->trash->unread ?></td>
                    <td class="center"><?php echo $this->mainFolders->trash->unread ?></td>
                    <td class=""><button style="margin-top: 0px;float: right;" onclick="clear_folder('trash', 'ゴミ箱')">空にする</button></td>
                    
                </tr>
                <?php
                    if(!empty($this->folders)){
                        foreach($this->folders as $folder){
                            echo 
                            "<tr class='row0'>" .
                                "<td>$folder->name</td>" .
                                "<td class='center'>". ($folder->read + $folder->unread) ."</td>" .
                                "<td class='center'>$folder->unread</td>" .
                                "<td class=''>" .
                                    "<button style='margin-top: 0px;float: left;' onclick='delete_folder($folder->code, \"$folder->name\")'>削除</button>" .
                                    "<button style='margin: 0 0 0 2px;' onclick='edit_folder($folder->code, \"$folder->name\", $folder->parent)'>更新</button>" .
                                    "<button style='margin-top: 0px;float: right;' onclick='clear_folder($folder->code, \"$folder->name\")'>空にする</button> " .
                                "</td>" .
                            "</tr>"
                            ;
                        }
                    } 
                ?>
        	</tbody>         
                    </table>                             
        </div>
</div>
    
 </div>
 <script>
        // show a dialog box when clicking on a link
    jq('#folder_add').bind('click', function(e){
        e.preventDefault();
        var tmpl =  "<div class='popup-new-folder'>" +
                        "<table>" +
                            "<tr>" +
                                "<td><label>フォルダ名</label></td>" +
                                "<td> <input id='create-folder-name' style='width:150px' type='text'/></td>" +
                            "</tr>" +
                            "<tr>" +
                                "<td><label>親フォルダ</label></td>" +
                                "<td>" +
                                    "<select id='create-folder-parent' style='width:150px'>" +
                                       "<option value='14'>Loading...</option>" +
                                    "</select>" +
                                "</td>" +
                            "</tr>" +
                        "</table>" +    
                    "</div>" ;
        zb = new jq.Zebra_Dialog(tmpl, {
            width: 300,
            title:  'フォルダを作成',
            type:  '',
            buttons:  [
                {caption: 'キャンセル', callback: function() { }},
                {caption: 'OK', callback: function(e){
                    if(jq('#create-folder-name').val() == ''){
                        return false;
                    }
                    jq.ajax({
                        dataType: 'json',
                        data:{
                            top: 'page', 
                            option:'com_frmessage', 
                            view: 'folder', 
                            task: 'createFolder', 
                            name: jq('#create-folder-name').val(), 
                            parent: jq('#create-folder-parent').val()
                        },
                        url: "index.php",
                        success: function(result) {
                            if(result.status == 'ok'){
                                location.reload();
                            } 
                        }
                    });
                } 
            }]
        });
        
        jq('#create-folder-parent').load('index.php?top=page&option=com_frmessage&view=folder&task=getFolderList');
        
    });
    
     function delete_folder(code, name){
        if(confirm('選択したフォルダを削除してもよろしいですか？削除した場合フォルダに含まれるメッセージも削除されます。')){
            jq.ajax({
                dataType: 'json',
                data:{
                    top: 'page', 
                    option:'com_frmessage', 
                    view: 'folder', 
                    task: 'DeleteFolder', 
                    code: code
                },
                url: "index.php",
                success: function(result) {
                    if(result.status == 'ok'){
                        location.reload();
                    }
                }
            });
        }
     }
     
     function edit_folder(code, name, parent){
        var tmpl =  "<div class='popup-new-folder'>" +
                        "<table>" +
                            "<tr>" +
                                "<td><label>フォルダ名</label></td>" +
                                "<td> <input id='create-folder-name' style='width:150px' type='text' value='" + name + "'/></td>" +
                            "</tr>" +
                            "<tr>" +
                                "<td><label>親フォルダ</label></td>" +
                                "<td>" +
                                    "<select id='create-folder-parent' style='width:150px'>" +
                                       "<option value='14'>Loading...</option>" +
                                    "</select>" +
                                "</td>" +
                            "</tr>" +
                        "</table>" +    
                    "</div>" ;
        zb = new jq.Zebra_Dialog(tmpl, {
            width: 300,
            title:  'フォルダを作成',
            type:  '',
            buttons:  [
                {caption: 'キャンセル', callback: function() { }},
                {caption: 'OK', callback: function(){
                    jq.ajax({
                        dataType: 'json',
                        data:{
                            top: 'page', 
                            option:'com_frmessage', 
                            view: 'folder', 
                            task: 'editFolder', 
                            name: jq('#create-folder-name').val(), 
                            code: code,
                            parent: jq('#create-folder-parent').val()
                        },
                        url: "index.php",
                        success: function(result) {
                            if(result.status == 'ok'){
                                //console.log('Folder edited!');
                                location.reload();
                            }
                        }
                    });
                } 
            }]
        });
        
        jq('#create-folder-parent').load('index.php?top=page&option=com_frmessage&view=folder&task=getFolderList', function(){
            jq('#create-folder-parent').val(parent);
            jq("#create-folder-parent option[value='" + code+ "']").remove();
        });
        
     }
     
     function clear_folder(code, name){
        if(confirm('選択したフォルダ内のデータを削除してもよろしいですか。')){
            jq.ajax({
                dataType: 'json',
                data:{
                    top: 'page', 
                    option:'com_frmessage', 
                    view: 'folder', 
                    task: 'clearFolder', 
                    code: code
                },
                url: "index.php",
                success: function(result) {
                  
                    if(result.status == 'ok'){
                        //console.log('Folder cleared!');
                        location.reload();
                    }
                }
            });
        }
     }
</script>
 <!-- END CONTENT -->
                           

